package com.gold.controller;

import com.alibaba.fastjson.JSONObject;
import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.domain.kplunion.GoodsService.request.query.JFGoodsReq;
import com.jd.open.api.sdk.domain.kplunion.GoodsService.request.query.MaterialGoodsReq;
import com.jd.open.api.sdk.domain.kplunion.GoodsService.response.query.JingfenQueryResult;
import com.jd.open.api.sdk.domain.kplunion.GoodsService.response.query.MaterialQueryResult;
import com.jd.open.api.sdk.request.kplunion.UnionOpenGoodsJingfenQueryRequest;
import com.jd.open.api.sdk.request.kplunion.UnionOpenGoodsMaterialQueryRequest;
import com.jd.open.api.sdk.response.kplunion.UnionOpenGoodsJingfenQueryResponse;
import com.jd.open.api.sdk.response.kplunion.UnionOpenGoodsMaterialQueryResponse;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author XiaoHao
 * @date 2022-04-14 13:34
 */
@Controller
@RequestMapping("api/jingdong")
public class JdController {
    private JdClient client = new DefaultJdClient("https://api.jd.com/routerjson", "", "64597f247a476f63ec18d9b05d969945", "785041f75411477e98848c54eea372b7");

    //京东联盟精选优质商品
    @RequestMapping("jingfen")
    @ResponseBody
    public String jingfen(@RequestBody JSONObject parm) {
        try {
            UnionOpenGoodsJingfenQueryRequest request = new UnionOpenGoodsJingfenQueryRequest();
            JFGoodsReq goodsReq = new JFGoodsReq();
            Object eliteId = parm.get("eliteId");
            Object pageIndex = parm.get("pageIndex");
            Object pageSize = parm.get("pageSize");
            //频道id
            goodsReq.setEliteId(Integer.valueOf(eliteId.toString()));
            //页码
            goodsReq.setPageIndex(Integer.valueOf(pageIndex.toString()));
            //每页数量
            goodsReq.setPageSize(Integer.valueOf(pageSize.toString()));
            request.setGoodsReq(goodsReq);
            request.setVersion("1.0");
            UnionOpenGoodsJingfenQueryResponse response = client.execute(request);
            JingfenQueryResult queryResult = response.getQueryResult();
            ObjectMapper mapper = new ObjectMapper();
            String string = mapper.writeValueAsString(queryResult);
            return string;
        } catch (Exception e) {
            return "后台错误,请联系管理员";
        }
    }

    //猜你喜欢
    @RequestMapping("material")
    @ResponseBody
    public String material(@RequestBody JSONObject parm) {
        try {
            UnionOpenGoodsMaterialQueryRequest request = new UnionOpenGoodsMaterialQueryRequest();
            MaterialGoodsReq goodsReq = new MaterialGoodsReq();
            Object eliteId = parm.get("eliteId");
            Object pageIndex = parm.get("pageIndex");
            Object pageSize = parm.get("pageSize");
            //频道id
            goodsReq.setEliteId(Integer.valueOf(eliteId.toString()));
            //页码
            goodsReq.setPageIndex(Integer.valueOf(pageIndex.toString()));
            //每页数量
            goodsReq.setPageSize(Integer.valueOf(pageSize.toString()));
            request.setGoodsReq(goodsReq);
            request.setVersion("1.0");
            UnionOpenGoodsMaterialQueryResponse response = client.execute(request);
            MaterialQueryResult queryResult = response.getQueryResult();
            ObjectMapper mapper = new ObjectMapper();
            String string = mapper.writeValueAsString(queryResult);
            return string;
        } catch (Exception e) {
            return "后台错误,请联系管理员";
        }

    }


}
